package com.example.twitter;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


public class ConfiguracionTwitter{
	
	public String consumerKey = "eX3HttjNder2Ihx9ymMxA";
	public String consumerSecret = "7zI8ySqh6MH8zpZ5s0OXutNDs6p7PN75scwJMLekNF4";
	
	public static final String PREFS = "MySettingsTwitter";
		
	public static Twitter twitter = null;
		
	public String token;
	public String tokenSecret;
	
	public AccessToken accessToken;
	
	
	
	public RequestToken rq;
	public ConfiguracionTwitter(Context context){
		accessToken = null;
		
		twitter = new TwitterFactory().getInstance();
		
		rq = null;
		
		twitter.setOAuthConsumer(consumerKey, consumerSecret);
		
		
		
		try{
			rq = twitter.getOAuthRequestToken();
		}catch(TwitterException e){
			
		}
	}
	
	public String getURLAuthent(){
		return rq.getAuthenticationURL();
	}
	
	public boolean ComprobarCredencialesLocales(Context context){
		SharedPreferences prefs = context.getSharedPreferences(PREFS, 0);
		if(prefs.contains("token"))
			return true;
		else
			return false;
	}
	
	
	public boolean ComprobarPIN(String PIN, Context context){
		try {
			accessToken = twitter.getOAuthAccessToken(rq, PIN);
			twitter.setOAuthAccessToken(accessToken);
			SharedPreferences prefs = context.getSharedPreferences(PREFS, 0);
			Editor editor = prefs.edit();
			editor.putString("token", accessToken.getToken());
			editor.putString("tokenSecret", accessToken.getTokenSecret());
			editor.commit();
			
		} catch (TwitterException e) {
			String m = e.getMessage();
			return false;
		}
		return true;
	}

	public boolean PublicarTweet(String Texto){
		
		try {
			Status stat = twitter.updateStatus(Texto);
			
		} catch (TwitterException e) {
			e.getErrorMessage();
			return false;
			
		}
		
		return true;
		
		
	}
	
	public void BorrarCredenciales(Context context){
		SharedPreferences prefs = context.getSharedPreferences(PREFS, 0);
		
		Editor editor = prefs.edit();
		editor.remove("token");
		editor.remove("tokenSecret");
		editor.commit();
	}
	
	public Status getTweet(long id){
		try {
			return twitter.showStatus(id);
		} catch (TwitterException e) {
			return null;
		}
		
	}

	
}

